import json

with open('array_data.json') as file:
    og_log = json.load(file)

proc_order_id_to_event_map = {}

for event in og_log:
    if event["type"] != "ORDER_EVENT":
        continue

    if "proc_order_id" not in event["event"]:
        continue

    proc_order_id = event["event"]["proc_order_id"]

    if proc_order_id not in proc_order_id_to_event_map:
        proc_order_id_to_event_map[proc_order_id] = {}

    type = event["event"]["type"]
    time = event["record_timestamp_human_readable"]

    proc_order_id_to_event_map[proc_order_id][type] = time

proc_order_id_to_event_map = {
    key:val for key, val in proc_order_id_to_event_map.items() if "ORDER_REJECTED" in val
}

import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime

# print(proc_order_id_to_event_map)

rows = []
for k, v in proc_order_id_to_event_map.items():
    row = {}

    row["proc_order_id"] = k
    row["submit_time"] = datetime.strptime(v["ORDER_SUBMITTED"], "%Y%m%d %H:%M:%S.%f")
    row["reject_time"] = datetime.strptime(v["ORDER_REJECTED"], "%Y%m%d %H:%M:%S.%f")

    rows.append(row)

df = pd.DataFrame(rows).set_index('proc_order_id')

df.sort_index(inplace=True)

# print(df)

fig = plt.figure()
ax1 = fig.add_subplot(111, ylabel="time", xlabel="proc_order_id")

df['submit_time'].plot(ax=ax1, color='r', lw=1., legend=True)
df['reject_time'].plot(ax=ax1, color='g', lw=1., legend=True)

plt.xticks(rotation = 7)
plt.subplots_adjust(bottom=.15, left=.15)
plt.savefig("submit_reject.png", dpi=200)

# print(df)
